package com.example.city_home_app.Activity;

import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.example.city_home_app.Bean.Down_Universe;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_MomentAdd;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_MomentComment;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.UIUtils;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.PicassoEngine;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MomentAddActivity extends BaseActivity {
    @BindView(R.id.tv_momentadd_submit)
    TextView tvMomentaddSubmit;
    @BindView(R.id.tv_momentadd_addpic)
    TextView tvMomentaddAddpic;
    @BindView(R.id.tv_momentadd_addvideo)
    TextView tvMomentaddAddvideo;
    @BindView(R.id.toolbar_moment_addmoment)
    Toolbar toolbarMomentAddmoment;
    @BindView(R.id.et_momentadd_content)
    EditText etMomentaddContent;
    @BindView(R.id.rec_momentaddimg)
    RecyclerView recMomentaddimg;

    private List<Uri> piclist = new ArrayList<>();
    private List<Uri> videopath = new ArrayList<>();
    private int pic_count=0;
    private boolean can_video = true,can_img = true;
    private Adapter_MomentAdd adapter_momentAdd;
    public static MomentAddActivity instance;
    private Dialog dialog;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_momentadd;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ButterKnife.bind(this);
        Window window = MomentAddActivity.this.getWindow();
        //如果系统5.0以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(0xff49afcd);
        }
        instance = this;
        showToast();

        recMomentaddimg = (RecyclerView) findViewById(R.id.rec_momentaddimg);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recMomentaddimg.setLayoutManager(layoutManager);
        adapter_momentAdd = new Adapter_MomentAdd(this);
        recMomentaddimg.setAdapter(adapter_momentAdd);
        recMomentaddimg.setItemAnimator(new DefaultItemAnimator());

        if (pic_count==0) {
            recMomentaddimg.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initData() {
        super.initData();
    }


    @OnClick({R.id.tv_momentadd_submit, R.id.tv_momentadd_addpic, R.id.tv_momentadd_addvideo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_momentadd_submit:
                if(!etMomentaddContent.getText().toString().equals("")){
                    upmoment();
                }else{
                    UIUtils.showToast(this, "请输入动态内容", UIUtils.LENGTH_SHORT);
                }
                break;
            case R.id.tv_momentadd_addpic:
                if(can_img){
                    addpic();
                }else{
                    UIUtils.showToast(this, "请选择上传多张图片或一个视频", UIUtils.LENGTH_SHORT);
                }
                break;
            case R.id.tv_momentadd_addvideo:
                if(can_video){
                    addvideo();
                }else{
                    UIUtils.showToast(this, "请选择上传多张图片或一个视频", UIUtils.LENGTH_SHORT);
                }
                break;
        }
    }

    private void addpic(){
        if (pic_count != 6) {
            Matisse.from(this)
                    .choose(MimeType.of(MimeType.JPEG, MimeType.PNG, MimeType.GIF))
                    .countable(true)
                    .maxSelectable(6 - pic_count)
                    .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                    .thumbnailScale(0.85f)
                    .theme(R.style.Matisse_Zhihu)
                    .imageEngine(new PicassoEngine())
                    .capture(false)
                    .forResult(100);
        } else {
            UIUtils.showToast(this, "最多上传六张图片", UIUtils.LENGTH_SHORT);
        }
    }

    private void addvideo(){
        Matisse.from(this)
                    .choose(MimeType.of(MimeType.MP4,MimeType.AVI))
                    .countable(true)
                    .maxSelectable(1)
                    .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                    .thumbnailScale(0.85f)
                    .theme(R.style.Matisse_Zhihu)
                    .imageEngine(new PicassoEngine())
                    .capture(false)
                    .forResult(101);
    }


    //1文字上传，2图文，3视频+文
    private void upmoment(){
        dialog = UIUtils.CreateLoadingDialog(this, "上传中...");
        ApiModel model = new ApiModel();
        int userid = BaseActivity.getUserinfo().getUserid();
        String content = etMomentaddContent.getText().toString();
        if(pic_count>0){
            //图文
            ArrayList<String> imgpath = new ArrayList<>();
            for(Uri i : piclist){
                imgpath.add(UIUtils.getFilePathFromUri(this,i));
            }
            model.upmoment(2,userid,content,imgpath,upmomentlistener);
        }else if(videopath.size()>0){
            //视频
            String path = UIUtils.getFilePathFromUri(this, videopath.get(0));
            ArrayList<String> imgpath = new ArrayList<>();
            imgpath.add(path);
            model.upmoment(3,userid,content,imgpath,upmomentlistener);
        }else{
            model.upmoment(1,userid,content,new ArrayList<String>(),upmomentlistener);
        }


    }


    //100为添加图片，101为视频
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (data != null) {
                if (requestCode == 100 && resultCode == RESULT_OK) {
                    List<Uri> list = Matisse.obtainResult(data);
                    piclist.addAll(list);
                    pic_count += list.size();
                    if(pic_count>0) {
                        can_video = false;
                        recMomentaddimg.setVisibility(View.VISIBLE);
                        adapter_momentAdd.settype(1);
                        adapter_momentAdd.setList(piclist);
                        adapter_momentAdd.notifyDataSetChanged();
                    }
                }else if(requestCode == 101 && resultCode == RESULT_OK){
                    videopath = Matisse.obtainResult(data);
                    can_video = false;
                    can_img = false;
                    recMomentaddimg.setVisibility(View.VISIBLE);
                    adapter_momentAdd.settype(2);
                    adapter_momentAdd.setList(videopath);
                    adapter_momentAdd.notifyDataSetChanged();
                }
            }
        }
    }

    private void showToast() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        toolbarMomentAddmoment = (Toolbar) findViewById(R.id.toolbar_moment_addmoment);
        setSupportActionBar(toolbarMomentAddmoment);
        toolbarMomentAddmoment.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setSupportActionBar(toolbarMomentAddmoment);
    }

    public void delimg(int position){
        piclist.remove(position);
        pic_count--;
        if(pic_count==0){
            can_video = true;
        }
        adapter_momentAdd.setList(piclist);
        adapter_momentAdd.notifyDataSetChanged();
    }

    public void delvideo(){
        can_video = true;
        can_img = true;
        videopath.clear();
        adapter_momentAdd.setList(piclist);
        adapter_momentAdd.notifyDataSetChanged();
    }

    private RetrofitListener<Down_Universe> upmomentlistener = new RetrofitListener<Down_Universe>() {
        @Override
        public void onResponse(Down_Universe listener) {
            if(listener.getMessage().equals("success")){
                dialog.dismiss();
                UIUtils.showToast(MomentAddActivity.this, "发布成功", UIUtils.LENGTH_SHORT);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };
}
